# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2020/11/19 17:16
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : Lagrange.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
from scipy.interpolate import lagrange
import numpy as np
import pylab as mpl
import matplotlib.pyplot as plt


def draw(data: np.ndarray, x_: np.ndarray, f: np.ndarray, n: int):
    ploy = lagrange(x_, f)
    f_x = [ploy(i) for i in data]
    x_range = np.linspace(-1, 1.2, 100)
    y_range = [ploy(i) for i in x_range]
    plt.scatter(data, f_x, label="插出点", color="green")
    plt.plot(x_range, y_range, label='拉格朗日插值函数', color='red')
    plt.title("%s次拉格朗日插值法结果" % n)
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    plt.legend(loc="lower right")
    plt.show()


if __name__ == '__main__':
    x = np.arange(0.2, 1.2, 0.2)
    w = np.array([0.98, 0.92, 0.81, 0.64, 0.38])
    i_set = np.array([0, 1, 10, 11])
    x_i = 0.08 * i_set + 0.2
    draw(x_i, x, w, 4)
